1569B - Chess Tournament - CodeForces Solution


constructive algorithms *1000

Please click on ads to support us..

Python Code:


def lastIndex(arr, x):
    for i in reversed(range(len(arr))):
        if(arr[i] == x):
            return i
        
def solve(n, exp):
    if exp.count(2) in range(1,3):
        print('NO')
        return
    else:
        board = [['=' for i in range(n)]for i in range(n)]
        for i in range(n):
            board[i][i] = 'X'
        if exp.count(2) != 0:
            
            firstIndexOf2 = exp.index(2)
            lastIndexOf2 = lastIndex(exp, 2)
            for i in range(firstIndexOf2, lastIndexOf2):
                if(exp[i] == 2):
                    for j in range(i+1, lastIndexOf2 + 1):
                        if(exp[j] == 2):
                            board[i][j] = '+'
                            board[j][i] = '-'
            board[lastIndexOf2][firstIndexOf2] = '+'
            board[firstIndexOf2][lastIndexOf2] = '-'
                
        print('YES')
        for i in range(n):
            print(''.join(board[i]))

tcs = int(input())
for i in range(tcs):
    n = int(input())
    e = input()
    exp = list(map(int, e))
    solve(n, exp)
    

    

C++ Code:

/*
**  Author - Md. Najmul Huda Hridoy
**  Handle - h3000plus
*/

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		string s;
		cin>>s;
		int two = 0;
		vector<int> idx;
		vector<vector<char>> ans(n, vector<char>(n));
		for(int i = 0; i < n; i++){
			if(s[i] == '2'){
				two++;
				idx.push_back(i);
			} 
		}
		
		if(two == 1 || two == 2){
			cout<<"NO"<<endl;
		}
		else{
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j) ans[i][j] = 'X';
					else ans[i][j] = '=';
				}
			}
			if(two > 2){
				int sz = idx.size() - 1;
				ans[idx[0]][idx[sz]] = '+';
				ans[idx[sz]][idx[0]] = '-';
				ans[idx[sz]][idx[sz - 1]] = '+';
				ans[idx[sz - 1]][idx[sz]] = '-';
				for(int i = 1; i < sz; i++){
					ans[idx[i]][idx[i - 1]] = '+';
					ans[idx[i - 1]][idx[i]] = '-';
				}
			}
			cout<<"YES"<<endl;
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					cout<<ans[i][j];
				}
				cout<<endl;
			}
		}
	}
	return 0;	
}


Comments

Submit
0 Comments
More Questions

1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array